<?php
namespace frontend\tool;
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019-4-7
 * Time: 11:38
 */

class Tool
{
    public static function sendRequest($url, $data, $options = array())
    {
        $result = null;
        $method = isset($options['method'])?$options['method']:"GET";
        if ( function_exists('curl_init') ) {
            $data = is_array($data)?http_build_query($data):$data;
            if ( strtoupper($method) == "GET" && !empty($data) ) {
                $url = stripos($url, "?") !== false?$url . "&" . $data:$url . "?" . $data;
            }
            $ch = curl_init();
            if ( isset($options['header']) && $options['header'] ) {
                curl_setopt($ch, CURLOPT_HEADER, $options['header']);
            }
            if ( isset($options['httpHeader']) && $options['httpHeader'] ) {
                curl_setopt($ch, CURLOPT_HTTPHEADER, $options['httpHeader']);
            }
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_URL, $url);
            if ( strtolower(substr($url, 0, 5)) == 'https' ) {

                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //不验证证书
                curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //不验证HOST
            }
            if ( isset($options['port']) && $options['port'] ) {
                curl_setopt($ch, CURLOPT_PORT, $options['port']);
            }
            if ( strtoupper($method) == 'POST' ) {
                curl_setopt($ch, CURLOPT_POST, true);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            }
            if ( isset($options['cookie']) && $options['cookie'] ) {
                curl_setopt($ch, CURLOPT_COOKIE, $options['cookie']);
            }

            $setoptConnectTimeOut = false;
            if ( isset($options['connectTimeOutMs']) && $options['connectTimeOutMs'] ) {
                $setoptConnectTimeOut = curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, $options['connectTimeOutMs']);
            } elseif ( isset($options['connectTimeOut']) && $options['connectTimeOut'] && !$setoptConnectTimeOut ) {
                curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $options['connectTimeOut']);
            }

            $setoptTimeOut = false;
            if ( isset($options['timeOutMs']) && $options['timeOutMs'] ) {
                $setoptTimeOut = curl_setopt($ch, CURLOPT_TIMEOUT_MS, $options['timeOutMs']);
            } elseif ( isset($options['timeOut']) && $options['timeOut'] && !$setoptTimeOut ) {
                $setoptTimeOut = curl_setopt($ch, CURLOPT_TIMEOUT, $options['timeOut']);
            } elseif ( !$setoptTimeOut ) {
                curl_setopt($ch, CURLOPT_TIMEOUT, 3);
            }
            if ( isset($options['userAgent']) && $options['userAgent'] ) {
                curl_setopt($ch, CURLOPT_USERAGENT, $options['userAgent']);
            }

            if ( isset($options['proxy']) && $options['proxy'] ) {
                if ( isset($options['proxy']['host']) && $options['proxy']['host'] ) {
                    curl_setopt($ch, CURLOPT_PROXY, $options['proxy']['host']); //代理服务器地址
                }
                if ( isset($options['proxy']['port']) && $options['proxy']['port'] ) {
                    curl_setopt($ch, CURLOPT_PROXYPORT, $options['proxy']['port']); //代理服务器端口
                }
            }
            $result = curl_exec($ch);

            $ret = array('state' => 1, 'data' => null, 'error' => null, 'errno' => null);
            if ( $result === FALSE ) {
                $ret['state'] = 0;
                $ret['error'] = curl_error($ch);
                $ret['errno'] = curl_errno($ch);
            } else {
                $ret['data'] = $result;
            }
            $result = &$ret;

            curl_close($ch);
        }
        return $result;
    }
    public static function getRandomStr()
    {
        $str = '';
        $str_pol = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
        $max = strlen($str_pol) - 1;

        for ($i = 0; $i < 5; $i++) {
            $str .= $str_pol[mt_rand(0, $max)];
        }
        return '用户' . $str;
    }

}